﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Data;

namespace UniMana.Win.Tool
{
    public partial class FrmSoftware_RequestForm_Transfer : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_dbTraining = new DBMain();
        int m_iSoftwareKey = 0;
        public string person = "";
        public int department = 0;

        public FrmSoftware_RequestForm_Transfer(int m_iSoftwareKey)
        {
            InitializeComponent();
            this.m_iSoftwareKey = m_iSoftwareKey;
        }

        private void FrmSoftware_RequestForm_Transfer_Load(object sender, EventArgs e)
        {
            gluCatalogue_Department.GetData(m_dbTraining.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name", noChoose: true);
            gluPersonnel_Personnel.GetData(m_dbTraining.m_DCReadOnly.Personnel_Personnels.GetListByDepartment(GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Department.EditValue)),
                "Personnel_PersonnelKey",
                "FullName");
        }

        /// <summary>
        /// Thay đổi giá trị người nhận
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluPersonnel_Personnel_EditValueChanged(object sender, EventArgs e)
        {
            Personnel_PersonnelInfo perinfo = new Personnel_PersonnelInfo(Guid.Parse(gluPersonnel_Personnel.EditValue.ToString()));
            gluCatalogue_Department.EditValue = perinfo.Properties.Catalogue_DepartmentKey;
        }

        /// <summary>
        /// Thay đổi giá trị Khoa / Phòng ban
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluCatalogue_DepartmentReceive_EditValueChanged(object sender, EventArgs e)
        {
            gluPersonnel_Personnel.GetData(m_dbTraining.m_DCReadOnly.Personnel_Personnels.GetListByDepartment(GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Department.EditValue)),
                "Personnel_PersonnelKey",
                "FullName");
        }

        /// <summary>
        /// Xác nhận và lưu thông tin chuyển quyền
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnOK_Click(object sender, EventArgs e)
        {
            if ((gluCatalogue_Department.EditValue == null) || (gluPersonnel_Personnel.EditValue == null))
            {
                XtraMessageBox.Show("Yêu cầu đầy đủ thông tin người nhận và phòng ban", "Thông báo");
                person = "";
                department = 0;
            }
            else
            {
                DialogResult result = XtraMessageBox.Show("Bạn có chắc muốn chuyển quyền yêu cầu cho " + gluPersonnel_Personnel.Text, "Thông báo", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    Software_RequestFormInfo info = new Software_RequestFormInfo(m_iSoftwareKey);
                    info.Properties.Personel_PersonelKeyRequest = Guid.Parse(gluPersonnel_Personnel.EditValue.ToString());
                    info.Properties.Catalogue_DepartmentKeySender = int.Parse(gluCatalogue_Department.EditValue.ToString());
                    info.Save();
                    person = gluPersonnel_Personnel.EditValue.ToString();
                    department = int.Parse(gluCatalogue_Department.EditValue.ToString());
                    XtraMessageBox.Show("Đã chuyển quyền.", "Thông báo");
                    this.Close();
                }
            }
        }

        /// <summary>
        /// Thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
